<template>
	<section>
		<!--高级查询-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="advancedQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toIn">导入</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleOut">导出</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleBatchAllocation">批量分配</el-button>
        </el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="fullName" label="名称" width="80px" sortable>
			</el-table-column>
      <el-table-column prop="appellation" label="称呼" width="80px" sortable>
      </el-table-column>
      <el-table-column prop="job" label="工作" width="100px" sortable>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="150px" sortable>
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="100px" sortable>
      </el-table-column>
      <el-table-column prop="address" label="地址" width="100px" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100px" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1" style="color: #5e7382">未分配</span>
          <span v-if="scope.row.state == 2" style="color: #50bfff">跟进中</span>
          <span v-if="scope.row.state == 3" style="color: lawngreen">已转化为商机</span>
          <span v-if="scope.row.state == 0" style="color: aquamarine">放入线索池</span>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="简介" width="200px" sortable>
      </el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleAppellation(scope.$index, scope.row)">关联活动</el-button>
					<el-button type="danger" v-if="scope.row.state == 1" size="small" @click="handleAllocation(scope.$index, scope.row)">分配</el-button>
					<el-button type="danger" v-if="scope.row.state == 2 || scope.row.state == 3" size="small" @click="handleChange(scope.$index, scope.row)">变更</el-button>
					<el-button type="danger" v-if="scope.row.state == 2 || scope.row.state == 0" size="small" @click="handleTransformation(scope.$index, scope.row)">转化商机</el-button>
					<el-button type="danger" v-if="scope.row.state == 2" size="small" @click="handleScrap(scope.$index, scope.row)">废弃</el-button>
				</template>
			</el-table-column>
		</el-table>

		<el-col :span="24" class="toolbar">
      <!--分页条-->
      <el-pagination
          style="float: right;margin: 20px"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.currentPage"
          :page-sizes="pageSizes"
          :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total">
      </el-pagination>
		</el-col>



		<!--保存界面-->
    <!--
       pros 绑定校验规则名称
       :visible.sync  控制dialog是否显示
       :rules  绑定表单校验规则
       ref="saveForm"  给当前表单取一个别名
    -->
		<el-dialog title="保存"
               :visible.sync="saveFormVisible"
               :close-on-click-modal="false">
			<el-form
          :model="saveForm"
          label-width="80px"
          :rules="saveFormRules"
          ref="saveForm">
				<el-form-item label="名称">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>

				<el-form-item label="售价">
					<el-input  v-model="saveForm.price"></el-input>
				</el-form-item>
        <el-form-item label="简介">
          <el-input  v-model="saveForm.description"></el-input>
        </el-form-item>
        <el-form-item label="资源地址">
          <el-input  v-model="saveForm.resource"></el-input>
        </el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
        <!--@click.native vue在自定义组件中使用原生事件要加上native-->
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">确认</el-button>
			</div>
		</el-dialog>

    <el-dialog title="批量分配"
               :visible.sync="batchAllocationFormVisible"
               :close-on-click-modal="false">
      <el-form
          :model="batchAllocationForm"
          label-width="80px"
          ref="batchAllocationForm">
        <el-form-item label="营销人员">
          <el-select v-model="batchAllocationForm.salesmanId" clearable value-key="id" placeholder="请选择">
            <!--下拉选项
              :key=""  //唯一标识
              :label 选择之后展示到选择框中的值
              :value 选中之后绑定给模型层的值  如果要绑定对象给模型层 有一个大坑
                 必须要写  value-key="id"
            -->
            <el-option
                v-for="e in employees"
                :key="e.id"
                :label="e.username"
                :value="e">
              <span style="float: left">{{ e.username }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!--@click.native vue在自定义组件中使用原生事件要加上native-->
        <el-button @click.native="batchAllocationFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="batchAllocationSave" :loading="batchAllocationLoading">确认</el-button>
      </div>
    </el-dialog>

    <el-dialog title="转化商机"
               :visible.sync="transformationFormVisible"
               :close-on-click-modal="false">
      <el-form
          :model="transformationForm"
          label-width="80px"
          ref="batchAllocationForm">
        <el-form-item label="线索名称">
          <el-input  v-model="transformationForm.fullName"></el-input>
        </el-form-item>
        <el-form-item label="线索电话">
          <el-input  v-model="transformationForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="线索邮箱">
          <el-input  v-model="transformationForm.email"></el-input>
        </el-form-item>
        <el-form-item label="产品信息">
          <el-select v-model="transformationForm.product" clearable value-key="id" placeholder="请选择">
            <!--下拉选项
              :key=""  //唯一标识
              :label 选择之后展示到选择框中的值
              :value 选中之后绑定给模型层的值  如果要绑定对象给模型层 有一个大坑
                 必须要写  value-key="id"
            -->
            <el-option
                v-for="item in products"
                :key="item.id"
                :label="item.name"
                :value="item">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!--@click.native vue在自定义组件中使用原生事件要加上native-->
        <el-button @click.native="transformationFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveTransformation" :loading="transformationLoading">确认</el-button>
      </div>
    </el-dialog>

    <el-dialog title="保存"
               :visible.sync="saveFormVisible"
               :close-on-click-modal="false">
      <el-form
          :model="saveForm"
          label-width="80px"
          :rules="saveFormRules"
          ref="saveForm">
        <el-form-item label="名称">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="售价">
          <el-input  v-model="saveForm.price"></el-input>
        </el-form-item>
        <el-form-item label="简介">
          <el-input  v-model="saveForm.description"></el-input>
        </el-form-item>
        <el-form-item label="资源地址">
          <el-input  v-model="saveForm.resource"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <!--@click.native vue在自定义组件中使用原生事件要加上native-->
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">确认</el-button>
      </div>
    </el-dialog>

    <el-dialog title="关联活动"
               :visible.sync="appellationFormVisible"
               :close-on-click-modal="false">
      <el-form
          :model="appellationForm"
          label-width="80px"
          ref="appellationForm">
        <el-form-item label="营销活动">
          <el-select v-model="appellationForm.activityIds" multiple clearable value-key="id" placeholder="请选择">
            <el-option
                v-for="item in mktActivity"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!--@click.native vue在自定义组件中使用原生事件要加上native-->
        <el-button @click.native="appellationFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveActivity" :loading="appellationLoading">确认</el-button>
      </div>
    </el-dialog>

    <el-dialog title="导入线索"
               :visible.sync="addClueFormVisible"
               :file-list="fileList"
               :close-on-click-modal="false">
      <el-upload
          class="upload-demo"
          action="http://localhost:9999/clue/importExcel"
          multiple
          :limit="3">
        <el-button size="small" type="primary">点击上传</el-button>
        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <!--@click.native vue在自定义组件中使用原生事件要加上native-->
        <el-button @click.native="addClueFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="handleIn" :loading="addClueLoading">确认</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>

	export default {
		data() {
			return {
        pageInfo:{
          rows: [],
          total: 0,
        },
        pageSizes:[5, 10, 20, 50],
        //查询对象
        query:{
				  //当前页,默认第一页
          currentPage:1,
          //页面显示数据大小,默认显示5条
          pageSize:5,
          //高级查询关键字
          keyword:""
        },
				listLoading: false,
				sels: [],//列表选中列

        saveFormVisible: false,//界面是否显示
        saveLoading: false,
        //校验规则
        saveFormRules: {
					name: [
						{ required: true, message: '请输入名称', trigger: 'blur' },
					],
          sn: [
            { required: true, message: '请输入标识', trigger: 'blur' }
          ]
				},
				//界面数据
        saveForm:{
          id:null,
          fullName:'',
          appellation:'',
          company:'',
          job:'',
          email:'',
          phone:'',
          mphone:'',
          address:'',
          source:null,
          owner:null,
          state:null,
          description:'',

          createBy:'',
          createTime:'',
          editBy:'',
          editTime:'',
          contactSummary:'',
          nextContactTime:'',

        },
        clueFormVisible:false,
        //批量分配
        batchAllocationFormVisible:false,
        batchAllocationLoading:false,
        batchAllocationForm:{
          salesmanId:null,
          ids:[]
        },
        employees:[],

        //转化商机
        transformationFormVisible:false,
        transformationLoading:false,
        transformationForm:{
          fullName:'',
          phone:'',
          email:'',
          product:''
        },
        products:[],

        //关联活动
        appellationFormVisible: false,
        appellationLoading:false,
        appellationForm:{
          clueId:null,
          activityIds:[]
        },
        mktActivity:[],

        //导入线索
        addClueFormVisible:false,
        addClueLoading:false,
      }
		},
		methods: {
		  //导入
      handleIn(){
        this.addClueFormVisible = false;
        this.getClues();

      },
      toIn(){
        this.addClueFormVisible = true;
      },
      //导出
      handleOut(){
        //location.href = "http://localhost:9999/clue/exportExcel2";
        window.open("http://localhost:9999/clue/exportExcel2")
        /*this.$http.post("/clue/exportExcel2")
            .then(result => {

            })
            .catch(result => {

            })*/
      },
		  //废弃
      handleScrap(){

      },
		  //转换商机
      handleTransformation(index,row){
        this.transformationFormVisible = true;
        this.transformationForm = row;
        this.getAllProduct();
      },
      //查询所有产品信息
      getAllProduct(){
        this.$http.get("/product")
            .then(result => {
              result = result.data;
              console.log(result)
              this.products = result.obj;
            })
      },
      //提交商机信息
      saveTransformation(){

        let para ={"clueId" : this.transformationForm.id,"productId" : this.transformationForm.product.id}
        console.log("商机信息");
        console.log(para);
        this.$http.post("/clue/businessTransformation",para)
            .then(result =>{
              result = result.data;
              if (result.success){
                this.$message({
                  message: result.message,
                  type: 'success'
                });
              }else {
                this.$message({
                  message: result.message,
                  type: 'error'
                });
              }
            })
            .catch(result =>{
              this.$message({
                message: '网络错误!',
                type: 'error'
              });
            })
        this.transformationFormVisible = false;
        this.advancedQuery();
      },
		  //变更销售人员
      handleChange(){

      },
		  //分配
      handleAllocation(){

      },
		  //批量分配
      handleBatchAllocation(){
        this.batchAllocationFormVisible = true;
        this.getAllEmployee();
      },
      //查询所有员
      getAllEmployee(){
        this.$http.get("/employee")
            .then(result => {
              result = result.data;
              console.log(result)
              this.employees = result.obj;
            })
      },
      //批量分配提交
      batchAllocationSave(){
        /*console.log("batchAllocationForm信息");
        console.log(this.batchAllocationForm);*/
        this.batchAllocationLoading = true;
        this.batchAllocationForm.salesmanId = this.batchAllocationForm.salesmanId.id;
        this.$http.post("/clue/batchAllocation",this.batchAllocationForm)
            .then(result =>{
              result = result.data;
              if (result.success){
                this.$message({
                  message: result.message,
                  type: 'success'
                });
              }else {
                this.$message({
                  message: result.message,
                  type: 'error'
                });
              }
            })
            .catch(result =>{
              this.$message({
                message: '网络错误!',
                type: 'error'
              });
            })
        this.advancedQuery();
        this.batchAllocationLoading = false;
        this.batchAllocationFormVisible = false;
      },
      //关联活动
      handleAppellation(index,row){
        this.appellationFormVisible = true;
        this.getActivity();
        this.appellationForm.clueId = row.id;
        //回显
        this.getByClueId();
      },
      getByClueId(){
        this.$http.get("/clueActivity/getByClueId/"+this.appellationForm.clueId)
            .then(result =>{
              result = result.data;
              this.appellationForm.activityIds = result.obj;

            })
            .catch(result =>{
              this.$message({
                message: '网络错误!',
                type: 'error'
              });
            })
      },
      //保存活动
      saveActivity(){
        /*console.log("保存活动信息")
        console.log(this.appellationForm)*/
        this.$http.put("/clueActivity/saveActivity",this.appellationForm)
            .then(result => {
              result = result.data;
              if (result.success){
                this.$message({
                  message: '关联成功!',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: result.message,
                  type: 'error'
                });
              }
            })
            .catch(result =>{
              this.$message({
                message: '网络错误!',
                type: 'error'
              });
            })
        this.appellationFormVisible = false;
      },
      //获取营销活动
      getActivity(){
        this.$http.get("/activity")
            .then(result =>{
              result = result.data;
              this.mktActivity = result.obj;

            })
            .catch(result =>{
              this.$message({
                message: '网络错误!',
                type: 'error'
              });
            })

      },
			handleCurrentChange(val) {
			  //改变当前显示页数
        this.query.currentPage = val;
        this.getClues();
			},
      handleSizeChange(val) {
			  //改变每页显示条数
        this.query.pageSize = val;
        this.getClues();
      },
			//获取列表
			getClues() {
				this.listLoading = false;
				this.$http.post("/clue",this.query)
        .then(result => {
          result = result.data;
          console.log(result.obj.total);
          console.log(result.obj.rows);
          this.pageInfo = result.obj;
        })
        .catch(result =>{
          this.$message({
            message: '网络错误!',
            type: 'error'
          });
        })
			},
      advancedQuery(){
        //初始化页面
        this.query.currentPage = 1;
        //加载数据到页面
        this.getClues();
      },
			//删除
			handleDel: function (index, row) {
        this.$confirm('数据将无法恢复!', '警告', {
          type: 'warning'
        }).then(() => {
          //弹出加载窗口
          this.listLoading = true;
          this.$http.delete("/clue/"+row.id)
              .then(result => {
                //操作成功进入then,关闭加载窗口
                this.listLoading = false;
                result = result.data;
                if(result.success){
                  this.$message({
                    message: '删除成功!',
                    type: 'success'
                  });
                  //删除完成后刷新页面
                  this.advancedQuery();
                }else{
                  this.$message({
                    message: '删除失败!',
                    type: 'error'
                  });
                }
              })
              .catch(result => {
                this.$message({
                  message: '网络错误!',
                  type: 'error'
                });
              })
        }).catch(() => {
          this.$message({
            message: '网络错误!',
            type: 'error'
          });
        });
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        //弹出界面
        this.saveFormVisible = true;
        this.saveForm = Object.assign({}, row);
        this.getTypeTree();
			},

			//显示新增界面
			handleAdd: function () {
        //弹出界面
        this.saveFormVisible = true;
        //初始化
        this.saveForm = {
          id:null,
          name:'',
          price: '',
          description:'',
          resource:'',
          typeId:''
        }
        this.getTypeTree();

      },
			//保存
      saveSubmit: function () {
        this.$refs.saveForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.saveLoading = true;
              //NProgress.start();
              let para = Object.assign({}, this.saveForm);

              if(para.typeId){
                para.typeId = para.typeId[para.typeId.length-1];
              }

              /*console.log("保存的数据")
              console.log(para)
              return ;*/
              this.$http.put("/clue",para)
                  .then(result => {
                    result = result.data;
                    if(result.success){
                      this.advancedQuery();

                      this.saveFormVisible = false;
                      this.$message({ message: '保存成功',type: 'success'});
                    }else{
                      this.$message({ message: result.message,type: 'error'});
                    }
                    this.saveLoading = false;

                  })
            });
          }
        });
			},
			selsChange: function (sels) {
				this.sels = sels;
				this.batchAllocationForm.ids = sels.map(x => x.id)
			},

		},
		mounted() {
			this.getClues();
		}
	}

</script>

<style scoped>

</style>